<?php

use tt\common\help\TableHelper;
use ttext\backend\cloud\models\CloudPlugin;
use wechat\models\WeConfig;
use wechat\models\WeContent;
use wechat\models\WeKeyword;
use wechat\models\WeUser;
use wechat\models\WeVar;
use wechat\Module;

class m201809_162605_wechat extends \yii\db\Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        if (!CloudPlugin::find()->where(['name' => Module::class])->exists()) {
            $plugin = new CloudPlugin();
            $plugin->attributes = [
                'title' => '微信后台',
                'name' => Module::class,
                'status' => CloudPlugin::STATUS_ACTIVE,
                'type' => CloudPlugin::TYPE_FREE,
                'version' => '1.0.0',
                'install_at' => time(),
                'expired' => 0,
                'entry' => 'wechat/default/index',
                'help' => 'wechat/help/index'
            ];
            $plugin->insert();
        }
        // 微信配置
        (new WeConfig())->createTable($this);
        // 微信用户表
        (new WeUser())->createTable($this);
        // 内容管理
        (new WeContent())->createTable($this);
        if (!TableHelper::columnExists(WeContent::tableName(), 'keyword')) {
            $this->addColumn(WeContent::tableName(), 'keyword', 'varchar(30) not null default "" after type');
            $this->createIndex('keyword', WeContent::tableName(), 'keyword');
        }
        // 关键词管理
        (new WeKeyword())->createTable($this);
        // 模板变量
        (new WeVar())->createTable($this);
    }
}
